package us.wili.dev.model.validator.valid;

import org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorContextImpl;
import us.wili.dev.model.validator.annotation.IncludeIntValue;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.lang.annotation.Annotation;

/**
 * @author wkx
 * @date 2018/9/18 11:08
 */
public class IntegerValidator implements ConstraintValidator<IncludeIntValue,Integer> {
    @Override
    public void initialize(IncludeIntValue constraintAnnotation) {

    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        if (value == null) {
            return false;
        }
        Annotation annotation = ((ConstraintValidatorContextImpl) context).getConstraintDescriptor().getAnnotation();
        IncludeIntValue intValue = (IncludeIntValue) annotation;
        for (int i : intValue.values()) {
            if (i == value) {
                return true;
            }
        }

        return false;
    }
}
